package servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import DAO.BaseDAO;
import DTO.Admin;
import serviceImpl.AdminDAO;
import util.UtilMD5;

@WebServlet(name = "zhuce",urlPatterns="/zhuce")
public class RegisteServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private static final String Base_DIR = "f:\\超市管理系统设计\\admin_photo";

    public RegisteServlet() {
    }

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //设置请求头数据字符集
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html; charset=UTF-8");
            request.setCharacterEncoding("utf-8");
            Date date = new Date();
            SmartUpload su = new SmartUpload();//创建上传组件对象
            su.initialize(this, request, response);
            su.setAllowedFilesList("jpg,jpeg,png,gif");//设置允许上传的文件类型
            su.setMaxFileSize(1024*1024*2);//设置单个文件的最大大小
            su.upload();//开始上传
            Files files = su.getFiles();//获取上传文件对象，可以看成是一个数组，但是并不是

            File file = files.getFile(0);//获取第一个文件对象
            String fname =null;
            try {
                if(file.getSize()>0){//判断文件大小
                    String ext = file.getFileExt();//获取文件后缀
                    fname = UUID.randomUUID() + "." + ext;//对文件进行重命名
                    System.out.println(Base_DIR + java.io.File.separator + fname);
                    file.saveAs(Base_DIR + java.io.File.separator + fname);//按照指定的路径对文件进行存储
                }
            }catch(Exception e){
                e.printStackTrace();
            }

            //获取页面上传来的表单数据
            String username = su.getRequest().getParameter("username").trim();//用户名
            String password = su.getRequest().getParameter("password").trim();//密码
            password = UtilMD5.MD5U(password);
            BaseDAO<Admin> ad = new AdminDAO();
            Admin admin = new Admin();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            admin.setId(UUID.randomUUID()+"");
            admin.setUsername(username);
            admin.setPassword(password);
            admin.setPhoto(fname);
            admin.setRegtime(sdf.format(date));
            String sql = "insert into admin(id,username,password,photo,regtime) values(?,?,?,?,?)";
            try {
                if(ad.add(admin,sql)){
                    //持久化成功   重定向   注册成功之后进入登录页面
                    request.setAttribute("msg","注册成功");
                    request.getRequestDispatcher("login.jsp").forward(request, response);
                }else{
                    //持久化失败  提示注册失败，用户名已存在
                    request.setAttribute("error", "用户名已经存在");
                    request.getRequestDispatcher("admin_rigiste.jsp").forward(request, response);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (SmartUploadException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        service(request, response);
    }

}
